package com.csti.delio.main.client.util;

import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;

public class Utilities {
	public static Integer itemsPerPage = 50;

	public static boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}

	public static boolean hasValue(NumberField field) {
		return field.getValue() != null
				&& field.getValue().toString().length() > 0;
	}

	public static String toIds(List<BeanModel> list) {
		String ids = "";
		for (BeanModel item : list) {
			ids += "," + item.get("id");
		}
		ids = ids.substring(1);
		return ids;
	}

	public static String toIds2(List<ModelData> list) {
		String ids = "";
		for (ModelData item : list) {
			ids += "," + item.get("id");
		}
		ids = ids.substring(1);
		return ids;
	}

	public static void setLengh(final TextField<String> txtNumeber,final TextField<String> txtField, final Integer lengh) {
		txtNumeber.addKeyListener(new KeyListener() {
			public void componentKeyDown(ComponentEvent event) {
				if (txtNumeber.getCursorPos() == lengh)
					txtField.focus();
			}

		});

	}

	public static void setLengh(final NumberField txtNumeber,
			final TextArea txtArea, final Integer lengh) {
		txtNumeber.addKeyListener(new KeyListener() {
			public void componentKeyDown(ComponentEvent event) {
				if (txtNumeber.getCursorPos() == lengh)
					txtArea.focus();
			}

		});

	}

	public static Double tipoCambio(String cadena) {
		Double res = 0.0;
		char[] array = cadena.toCharArray();

		String numero1 = "";
		String numero2 = "";
		int aux = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] == '*')
				aux = i;
			if (array[i] == ',')
				array[i] = '.';
		}

		for (int i = 0; i < array.length; i++) {
			if (i < aux)
				numero1 += array[i];
			if (i > aux)
				numero2 += array[i];
		}

		if (aux!=0){
			Double n1 = Double.valueOf(numero1);
			Double n2 = Double.valueOf(numero2);
			res = n1 * n2;
		}

		return res;

	}

	public static void setNumerosDouble(final TextField<String> txtField) {

		txtField.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if ((c > 31 && (c < 48 || c > 57) && c != 46 && c != 42))
					event.preventDefault();

			}

		});

	}
	public static void setNumerosEnteros(final TextField<String> txtField) {

		txtField.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if (c > 31 && (c < 48 || c > 57) )
					event.preventDefault();

			}

		});

	}
}
